package com.java46.carera.position;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java46.carera.base.entity.Department;
import com.java46.carera.base.entity.JsonResult;
import com.java46.carera.base.entity.Positi;
import com.java46.carera.base.mapper.PositiMapper;
import com.java46.carera.base.util.JsonUtil;
import com.java46.carera.department.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.util.List;

@Service
public class PositionService {
    public PositionService(){System.out.println("positionService...");}

    @Autowired
    public PositiMapper positiMapper;

    @Autowired
    public DepartmentService departmentService;

    // 添加
    public void addPosition( Integer staff_department, String position_name, String position_introduction){
        Department department = departmentService.selectDepartmentStaff(staff_department);

        Positi positi = new Positi();
        positi.setPosname(position_name);
        positi.setPosbrief(position_introduction);

        Department department1 = new Department();
        department1.setDepid(department.getDepid());
        department1.setDepname(department.getDepname());
        department1.setDepbrief(department.getDepbrief());
        positi.setDepid(department1);

        int addPosition = positiMapper.insert(positi);
        if (addPosition > 0){
            JOptionPane.showMessageDialog(null, "添加成功！");
        }else{
            JOptionPane.showMessageDialog(null, "添加失败！");
        }
    }

    // 删除
    public void delPosition(Integer position_id){
        int delPosition = positiMapper.deleteByPrimaryKey(position_id);
        if (delPosition > 0){
            JOptionPane.showMessageDialog(null, "删除成功！");
        }else{
            JOptionPane.showMessageDialog(null, "删除失败！");
        }
    }

    // 修改前的查询
    public JsonResult selectPosition(Integer position_id){
        Positi positi = positiMapper.selectByPrimaryKey(position_id);
        return JsonUtil.setJsonResult(true, "",positi);
    }

    // 修改
    public void uplPosition(Integer position_id, String position_name, String position_introduction, Integer staff_department){
        Department department = departmentService.selectDepartmentStaff(staff_department);
        Positi positi = new Positi();
        positi.setPosname(position_name);
        positi.setPosbrief(position_introduction);

        Department department1 = new Department();
        department1.setDepid(department.getDepid());
        department1.setDepname(department.getDepname());
        department1.setDepbrief(department.getDepbrief());
        positi.setDepid(department1);

        positi.setPosid(position_id);

        int uplPosition = positiMapper.updateByPrimaryKey(positi);
        if (uplPosition > 0){
            JOptionPane.showMessageDialog(null, "修改成功！");
        }else{
            JOptionPane.showMessageDialog(null, "修改失败！");
        }
    }

    // 查询
    public JsonResult selectPositionAll(Integer pageNum, String posName){
        //获取第1页，5条内容，默认查询总数count
        PageHelper.startPage(pageNum,4);
        List<Positi> selectPosition = positiMapper.selectAll(posName);
        PageInfo<Positi> page = new PageInfo<>(selectPosition);
        return JsonUtil.setJsonResult(true, "",page);
    }

    // 查询
//    public JsonResult selectPositionAllStaff(){
//        List<Positi> selectPosition = positiMapper.selectAll();
//        return JsonUtil.setJsonResult(true, "",selectPosition);
//    }

    // 用于员工和职位的查询
    public Positi selectPositiStaff(Integer position_id){
        Positi positi = positiMapper.selectByPrimaryKey(position_id);
        return positi;
    }

    public JsonResult selectDepPosition(Integer department_id){
        List<Positi> positis = positiMapper.selectPosit(department_id);
        return JsonUtil.setJsonResult(true, "",positis);
    }
}
